//
//  PMineResumeSelfDescriptionViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/24.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineResumeSelfDescriptionViewController.h"

@interface PMineResumeSelfDescriptionViewController ()
{
    PSTextView *selfDescriptionTextView;
}
@end

@implementation PMineResumeSelfDescriptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = self.titleStr;
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    selfDescriptionTextView = [[PSTextView alloc] initWithFrame:CGRectMake(30*Scale, 30*Scale, WIDTH-60*Scale, ((self.limitTextMaxNumber)?300*Scale:225*Scale)) leftText:@"" text:((self.context&&self.context.length)?self.context:nil) textColor:THEMECOLOR_OF_TEXT_333333 placeholder:[NSString stringWithFormat:@"请填写%@", self.titleStr] font:Font_15 editable:YES limitMax:((self.limitTextMaxNumber)?self.limitTextMaxNumber:LimitText_Max_Number) target:self showNumber:YES];
    [self.view addSubview:selfDescriptionTextView];
    if (self.context&&self.context.length) {
        selfDescriptionTextView.textView.text = self.context;
    }
}
- (void)okBtnTap {
    if (self.mineSpecialtyFlag) {
        if (selfDescriptionTextView.textView.text.length) {
            SVPHUDShow;
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
            dic = [UIToolsControl getPublicParameter];
            [dic setObject:selfDescriptionTextView.textView.text forKey:@"specialty"];
            [BaseNetManager requestWithMethod:POST path:Person_SaveSpecialty_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
                if ([responseObject[@"code"] integerValue] == 2000) {
                    SVPHUDSuccess(responseObject[@"msg"]);
                    if (self.delegate && [self.delegate respondsToSelector:@selector(pMineResumeSelfDescriptionViewControllerBack:)]) {
                        [self.delegate pMineResumeSelfDescriptionViewControllerBack:self->selfDescriptionTextView.textView.text];
                    }
                    [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
                } else {
                    SVPHUDError(responseObject[@"msg"]);
                }
            } failurBlock:^(NSError * _Nonnull error) {
                SVPHUDError(NetErrorAlertText);
            }];
        } else {
            SVPHUDError(@"您还没填写，请填写");
        }
    } else {
        if (self.limitTextMaxNumber) {
            if (selfDescriptionTextView.textView.text.length) {
                [selfDescriptionTextView.textView resignFirstResponder];
                if (self.delegate && [self.delegate respondsToSelector:@selector(pMineResumeSelfDescriptionViewControllerBack:)]) {
                    [self.delegate pMineResumeSelfDescriptionViewControllerBack:selfDescriptionTextView.textView.text];
                }
                
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [selfDescriptionTextView.textView becomeFirstResponder];
                SVPHUDError(SelfDescriptionAlertText);
            }
        } else {
            if (selfDescriptionTextView.textView.text.length) {
                [selfDescriptionTextView.textView resignFirstResponder];
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(pMineResumeSelfDescriptionViewControllerBack:)]) {
                    [self.delegate pMineResumeSelfDescriptionViewControllerBack:selfDescriptionTextView.textView.text];
                }
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [selfDescriptionTextView.textView becomeFirstResponder];
                SVPHUDError(SelfDescriptionAlertText);
            }
        }
    }
}
- (void)delayClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [selfDescriptionTextView.textView resignFirstResponder];
}
- (void)customBackButtonTap {
    if (selfDescriptionTextView.textView.text.length) {
        CustomView *alertView = [[CustomView alloc] initWithTitle:@"提示" message:@"当前页内容还没有保存，您确认要离开吗？" prompt:nil sureBtn:@"确定" cancleBtn:@"点错了" size:170];
        alertView.resultIndex = ^(NSInteger index){ //回调---处理一系列动作
            if (index == 2) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        };
        [alertView showXLAlertView];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
